package com.cs456.TeamBobcat.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class MyDB {
    private SQLiteDatabase db;
    private final Context context;
    private final MyDBhelper dbhelper;
    public MyDB(Context c){
        context = c;
        dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
                                           Constants.DATABASE_VERSION);
    }
    public void close()
    {
        db.close();
    }
    public void open() throws SQLiteException
    {
        try {
            db = dbhelper.getWritableDatabase();
        } catch(SQLiteException ex) {
            Log.v("Open database exception caught", ex.getMessage());
            db = dbhelper.getReadableDatabase();
        }
    }
    public long insertphotos(String title, String content)
    {
        try{
            ContentValues newTaskValue = new ContentValues();
            newTaskValue.put(Constants.TITLE_NAME, title);
            newTaskValue.put(Constants.CONTENT_NAME, content);
            newTaskValue.put(Constants.DATE_NAME,
            				java.lang.System.currentTimeMillis());
            return db.insert(Constants.TABLE_NAME, null, newTaskValue);
        } catch(SQLiteException ex) {
            Log.v("Insert into database exception caught",
                  ex.getMessage());
            return -1;
        }
    }
    
    public long insertgps(double x, double y)
    {
        try{
            ContentValues newTaskValue = new ContentValues();
            newTaskValue.put(Constants.GPS_X,x);
            newTaskValue.put(Constants.GPS_Y,y);
            newTaskValue.put(Constants.DATE_NAME,
                    		java.lang.System.currentTimeMillis());
            return db.insert(Constants.GPS_NAME, null, newTaskValue);
        } catch(SQLiteException ex) {
            Log.v("Insert into database exception caught",
                  ex.getMessage());
            return -1;
        }
    }
    
    public long insertWDTSphotos(String content, String resultText)
    {
        try{
            ContentValues newTaskValue = new ContentValues();
            newTaskValue.put(Constants.WDTS_TEXT, resultText);
            newTaskValue.put(Constants.CONTENT_NAME, content);
            newTaskValue.put(Constants.DATE_NAME,
            				java.lang.System.currentTimeMillis());
            return db.insert(Constants.WDTS_NAME, null, newTaskValue);
        } catch(SQLiteException ex) {
            Log.v("Insert into database exception caught",
                  ex.getMessage());
            return -1;
        }
    }
    public long insertAudio(String audioPath, String resultText)
    {
        try{
            ContentValues newTaskValue = new ContentValues();
            newTaskValue.put(Constants.AUDIO_PATH, audioPath);
            newTaskValue.put(Constants.AUDIO_TEXT, resultText);
            newTaskValue.put(Constants.DATE_NAME,
            				java.lang.System.currentTimeMillis());
            return db.insert(Constants.AUDIO_TABLE_NAME, null, newTaskValue);
        } catch(SQLiteException ex) {
            Log.v("Insert into database exception caught",
                  ex.getMessage());
            return -1;
        }
    }
    
    public Cursor getphotos()
    {
        Cursor c =  db.query(Constants.TABLE_NAME, null, null,
                             null, null, null, null);
        return c;
    }
    public Cursor getgps()
    {
        Cursor c =  db.query(Constants.GPS_NAME, null, null,
                             null, null, null, null);
        return c;
    }
    public Cursor getWDTSphotos()
    {
        Cursor c =  db.query(Constants.WDTS_NAME, null, null,
                             null, null, null, null);
        return c;
    }
    public Cursor getAudio()
    {
        Cursor c =  db.query(Constants.AUDIO_TABLE_NAME, null, null,
                             null, null, null, null);
        return c;
    }
}

